package com.team622.shop.orders.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.team622.shop.common.exceptions.BusinessException;
import com.team622.shop.common.exceptions.enums.Resp;
import com.team622.shop.orders.model.entity.Payment;
import com.team622.shop.orders.service.mapper.PaymentMapper;
import com.team622.shop.orders.service.service.PaymentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author team622
 * @since 2025-07-16
 */
@Service
public class PaymentServiceImpl extends ServiceImpl<PaymentMapper, Payment> implements PaymentService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Payment addPayment(Payment payment) {
        // 设置默认值
        if (payment.getPaymentStatus() == null) {
            payment.setPaymentStatus(0); // 默认未支付
        }
        if (payment.getIsDeleted() == null) {
            payment.setIsDeleted(0);
        }
        payment.setCreatedAt(new Date());
        payment.setUpdatedAt(new Date());

        this.save(payment);
        return payment;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deletePayment(Long id) {
        if (id == null) {
            throw new BusinessException(Resp.INVALID_PARAM);
        }
        return this.removeById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Payment updatePayment(Payment payment) {
        if (payment == null || payment.getId() == null) {
            throw new BusinessException(Resp.INVALID_PARAM);
        }
        payment.setUpdatedAt(new Date());
        this.updateById(payment);
        return payment;
    }

    @Override
    public Payment getPaymentById(Long id) {
        if (id == null) {
            throw new BusinessException(Resp.INVALID_PARAM);
        }
        return this.getById(id);
    }

    @Override
    public Payment getPaymentByOrderId(Long orderId) {
        if (orderId == null) {
            throw new BusinessException(Resp.INVALID_PARAM);
        }
        LambdaQueryWrapper<Payment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Payment::getOrderId, orderId)
                .eq(Payment::getIsDeleted, 0);
        return this.getOne(queryWrapper);
    }

    @Override
    public IPage<Payment> pagePayment(Integer current, Integer pageSize, Long orderId, String orderNo, String transactionId) {
        LambdaQueryWrapper<Payment> queryWrapper = new LambdaQueryWrapper<>();
        
        // 按订单ID查询
        if (orderId != null) {
            queryWrapper.eq(Payment::getOrderId, orderId);
        }
        
        // 按订单号查询
        if (StringUtils.hasText(orderNo)) {
            queryWrapper.eq(Payment::getOrderNo, orderNo);
        }
        
        // 按交易流水号查询
        if (StringUtils.hasText(transactionId)) {
            queryWrapper.eq(Payment::getTransactionId, transactionId);
        }
        
        // 未删除的记录
        queryWrapper.eq(Payment::getIsDeleted, 0);
        
        // 按创建时间倒序
        queryWrapper.orderByDesc(Payment::getCreatedAt);
        
        return this.page(new Page<>(current, pageSize), queryWrapper);
    }
}
